using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum SwordType
{
    Regular,//普通
    Bounce,//反弹
    Pierce,//穿刺
    Spin,//旋转
}

public class Sword_Skill : Skill
{
    public SwordType swordType = SwordType.Regular;

    [Header("反弹信息 Bounce info")]
    [SerializeField] private UI_SkillTreeSlot bounceUnlockButton;
    [Tooltip("反弹次数")]
    [SerializeField] private int bounceAmount = 4;
    [Tooltip("反弹重力")]
    [SerializeField] private float bounceGravity = 1;
    [Tooltip("反弹速度")]
    [SerializeField] private float bounceSpeed = 20;

    [Header("穿刺信息 Pierce info")]
    [SerializeField] private UI_SkillTreeSlot pierceUnlockButton;
    [Tooltip("穿刺个数")]
    [SerializeField] private int pierceAmount = 4;
    [Tooltip("穿刺重力")]
    [SerializeField] private float PierceGravity = 1;

    [Header("旋转Spin info")]
    [SerializeField] private UI_SkillTreeSlot spinUnlockButton;
    [Tooltip("旋转伤害间隔")]
    [SerializeField] private float hitCooldown = 0.35f;
    [Tooltip("最大距离")]
    [SerializeField] private float maxTravelDistance = 7;
    [Tooltip("旋转时间")]
    [SerializeField] private float spinDuration = 2;
    [Tooltip("旋转重力")]
    [SerializeField] private float spinGravity = 1;

    [Header("技能信息 Skill info")]
    [SerializeField] private UI_SkillTreeSlot swordUnlockButton;
    public bool swordUnlocked { get; private set; }
    [Tooltip("剑的预制体")]
    [SerializeField] private GameObject swordPrefab;
    [Tooltip("剑的起飞方向")]
    [SerializeField] private Vector2 launchDir;
    [Tooltip("剑的重力")]
    [SerializeField] private float swordGravity;
    [Tooltip("返回速度")]
    [SerializeField] private float returnSpeed = 12;
    [Tooltip("冻结时间")]
    [SerializeField] private float freezeTimeDuration = .7f;

    [Header("Passive skills")]
    [SerializeField] private UI_SkillTreeSlot timeStopUnlockButton;
    public bool timeStopUnlocked { get; private set; }
    [SerializeField] private UI_SkillTreeSlot vulnerableUnlockButton;
    public bool vulnerableUnlocked { get; private set; }

    private Vector2 finalDir;

    [Header("瞄准信息 Aim dots")]
    [Tooltip("瞄准点数")]
    [SerializeField] private int numberOfDots;
    [Tooltip("瞄准点间隔时间")]
    [SerializeField] private float spaceBeetwenDots;// 抛物线
    [Tooltip("瞄准点预制体")]
    [SerializeField] private GameObject dotPrefab;
    [Tooltip("瞄准点父结点")]
    [SerializeField] private Transform dotsParent;

    private GameObject[] dots;// 瞄准点数组，以及数量不用动态数组

    protected override void Start()
    {
        base.Start();
        GenereateDots();// 生成抛物线点
        SetupGraivty();// 设置剑的重力,可以放在Update中，在这更换不会刷新

        swordUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockSword); // 普通剑解锁按钮
        bounceUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockBounceSword); // 弹跳剑解锁按钮
        pierceUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockPierceSword); // 穿透剑解锁按钮
        spinUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockSpinSword); // 旋转剑解锁按钮
        timeStopUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockTimeStop); // 时间停止技能解锁按钮
        vulnerableUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockVulnurable); // 脆弱状态技能解锁按钮
    }

    protected override void Update()
    {
        if (Input.GetKeyUp(KeyCode.Mouse1))// 瞄准状态抬起鼠标右键
        {
            // 根据鼠标位置和投掷力度计算扔的方向
            finalDir = new Vector2(AimDirection().normalized.x * launchDir.x, AimDirection().normalized.y * launchDir.y);
        }
        if (Input.GetKey(KeyCode.Mouse1))// 持续按住鼠标右键
        {
            for (int i = 0; i < dots.Length; i++)
            {
                // 按住鼠标的时候不断为抛物线瞄准点生成位置
                dots[i].transform.position = DotsPosition(i * spaceBeetwenDots);
            }
        }
    }

    // 创造剑
    public void CreateSword()
    {
        //                                   预制体          生成位置                生成角度
        GameObject newSword = Instantiate(swordPrefab, player.transform.position, transform.rotation);
        Sword_Skill_Controller newSwordScript = newSword.GetComponent<Sword_Skill_Controller>();

        // 设置投剑的类型
        switch (swordType)
        {
            case SwordType.Bounce:
                newSwordScript.SetupBounce(true, bounceAmount, bounceSpeed);// 反弹
                break;
            case SwordType.Pierce:
                newSwordScript.SetupPierce(pierceAmount);// 刺穿
                break;
            case SwordType.Spin:
                newSwordScript.SetupSpin(true, maxTravelDistance, spinDuration, hitCooldown);// 旋转
                break;
        }
        // 设置剑的方向力度、重力、玩家、冻结时间、返回速度
        newSwordScript.SetupSword(finalDir, swordGravity, player, freezeTimeDuration, returnSpeed);
        player.AssignNewSword(newSword);// 让玩家获取剑的实例
        DotsActive(false);// 将剑丢出去后隐藏瞄准抛物线
    }

    // 设置剑的重力
    private void SetupGraivty()
    {
        switch (swordType)
        {
            case SwordType.Bounce: { swordGravity = bounceGravity; break; }
            case SwordType.Pierce: { swordGravity = PierceGravity; break; }
            case SwordType.Spin: { swordGravity = spinGravity; break; }
        }
    }

    #region Aim 瞄准 
    //根据鼠标和玩家位置计算向量，方面得到合适的单位向量作为扔出去的角度
    public Vector2 AimDirection()
    {
        Vector2 playerPosition = player.transform.position;// 获取玩家位置
        Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);// 获取鼠标位置
        Vector2 direction = mousePosition - playerPosition;// 返回位置
        return direction;
    }

    //显示/隐藏抛物线
    public void DotsActive(bool _isActive)
    {
        for (int i = 0; i < dots.Length; i++)
        {
            dots[i].SetActive(_isActive);
        }
    }

    // 生成抛物线点
    private void GenereateDots()
    {
        dots = new GameObject[numberOfDots];
        for (int i = 0; i < numberOfDots; i++)
        {
            dots[i] = Instantiate(dotPrefab, player.transform.position, Quaternion.identity, dotsParent);
            dots[i].SetActive(false);
        }
    }

    // 给每个点根据时间生成对应的位置（抛物线）
    private Vector2 DotsPosition(float t)
    {
        Vector2 position = (Vector2)player.transform.position + new Vector2(
            AimDirection().normalized.x * launchDir.x,
            AimDirection().normalized.y * launchDir.y) * t + .5f * (Physics2D.gravity * swordGravity) * (t * t);
        return position;

    }

    #endregion

    #region 相关技能解锁

    // 解锁时间停止技能
    private void UnlockTimeStop()
    {
        if (timeStopUnlockButton.unlocked)
            timeStopUnlocked = true; // 设置时间停止技能已解锁
    }

    // 解锁脆弱状态技能
    private void UnlockVulnurable()
    {
        if (vulnerableUnlockButton.unlocked)
            vulnerableUnlocked = true; // 设置脆弱状态技能已解锁
    }

    // 解锁普通剑技能
    private void UnlockSword()
    {
        if (swordUnlockButton.unlocked)
        {
            swordUnlocked = true; // 设置普通剑技能已解锁
            swordType = SwordType.Regular; // 设置剑类型为普通剑
        }
    }

    // 解锁弹跳剑技能
    private void UnlockBounceSword()
    {
        if (bounceUnlockButton.unlocked)
            swordType = SwordType.Bounce; // 设置剑类型为弹跳剑
    }

    // 解锁穿透剑技能
    private void UnlockPierceSword()
    {
        if (pierceUnlockButton.unlocked)
            swordType = SwordType.Pierce; // 设置剑类型为穿透剑
    }

    // 解锁旋转剑技能
    private void UnlockSpinSword()
    {
        if (spinUnlockButton.unlocked)
            swordType = SwordType.Spin; // 设置剑类型为旋转剑
    }


    #endregion

    protected override void CheckUnlock()
    {
        UnlockTimeStop();
        UnlockVulnurable();
        UnlockSword();
        UnlockBounceSword();
        UnlockPierceSword();
        UnlockSpinSword();
    }

}
